// Copyright © yanghy. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and limitations under the License.

package designer

import (
	"github.com/energye/designer/consts"
	"github.com/energye/designer/pkg/logs"
	"github.com/energye/lcl/lcl"
	"github.com/energye/lcl/types"
	"unsafe"
)

// 设计 - 组件树 - 事件

// 组件树右键菜单
func (m *FormTab) TreeOnContextPopup(sender lcl.IObject, mousePos types.TPoint, handled *bool) {
	logs.Debug("TreeOnContextPopup mousePos:", mousePos)
	node := m.tree.Selected()
	if node != nil && node.IsValid() {
		data := node.Data()
		component := m.DataToDesigningComponent(data)
		if component != nil && component.ComponentType == consts.CtForm {
			*handled = true
		}
	}
}

// 组件树鼠标按下事件
func (m *FormTab) TreeOnMouseDown(sender lcl.IObject, button types.TMouseButton, shift types.TShiftState, X int32, Y int32) {
	logs.Debug("TreeOnMouseDown x:", X, "y:", Y)
	if button == types.MbRight {
		selectNode := m.tree.GetNodeAt(X, Y)
		if selectNode != nil && selectNode.IsValid() {
			m.tree.SetSelected(selectNode)
		}
	}
}

// 数据指针转设计组件
func (m *FormTab) DataToDesigningComponent(data uintptr) *TDesigningComponent {
	dc := (*TDesigningComponent)(unsafe.Pointer(data))
	return dc
}

// 组件树选择事件
func (m *FormTab) TreeOnGetSelectedIndex(sender lcl.IObject, node lcl.ITreeNode) {
	data := node.Data()
	component := m.DataToDesigningComponent(data)
	if component != nil {
		m.switchComponentEditing(component)
	}
	logs.Info("Inspector-component-tree OnGetSelectedIndex name:", node.Text(), "id:", component.id)
}

// 取消选中所有节点
//func (m *InspectorComponentTree) UnSelectedAll() {
//	for _, node := range m.nodeData {
//		node.node.SetSelected(false)
//	}
//}
